﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using OxyPlot;

namespace PlayOxyPlot
{
    public partial class DynamicWaveView : UserControl
    {
        private readonly long _startTime;

        public DynamicWaveView()
        {
            InitializeComponent();

            var model = new PlotModel("Dynamic Waves");
            model.Axes.Add(new LinearAxis(AxisPosition.Left, -2, 2));
            for (int i = 0; i < 20; i++)
            {
                model.Series.Add(new LineSeries{LineStyle = LineStyle.Solid});
            }
            plot.Model = model;

            _startTime = DateTime.Now.Ticks;
            CompositionTarget.Rendering += CompositionTargetOnRendering;
        }

        private void CompositionTargetOnRendering(object sender, EventArgs eventArgs)
        {
            var t = new TimeSpan(DateTime.Now.Ticks - _startTime).TotalMilliseconds * 0.001;

            for (int index = 0; index < plot.Model.Series.Count; index++)
            {
                var series = plot.Model.Series[index] as LineSeries;
                series.Points.Clear();

                double a = 0.5 + index * 0.05;
                for (double x = -5; x <= 5; x += 0.01)
                {
                    double y = Math.Cos(t * a) * (x == 0 ? 1 : Math.Sin(x * a) / x);
                    series.Points.Add(new DataPoint(x, y));
                }
            }//for

            plot.Model.RefreshPlot(true);
        }
    }
}
